import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:wallet_utils/color.dart';
import 'package:wallet_utils/log_util.dart';
import 'package:wallet_utils/muti_image_util.dart';
import 'package:image_picker/image_picker.dart';
import 'package:wallet_utils/report_widget/base/callback/base_callback.dart';
import 'package:wallet_utils/report_widget/loading_utils.dart';
import 'package:wallet_utils/toast.dart';
import '../api/mine_api.dart';
import '../bean/sign_agreement_bean.dart';
import '../bean/updateImageBean.dart';
import 'state.dart';

class ContactServiceLogic extends GetxController {
  final ContactServiceState state = ContactServiceState();

  void feedbackCheck(int index) {
    for (int i = 0; i < state.list.length; i++) {
      if (index == i) {
        state.question=state.list[i].name;
        state.list[i].nameColor = ColorManager.colorD93C3A;
        state.list[i].typeColor = ColorManager.colorFFFFFF;
        state.list[i].bordColor = ColorManager.colorD93C3A;
      } else {
        state.list[i].nameColor = ColorManager.color333333;
        state.list[i].typeColor = ColorManager.colorF8F9FA;
        state.list[i].bordColor = ColorManager.colorF8F9FA;
      }
    }
    update();
  }

  ///相册上传
  void openGallery(context) async {
    try {
      String? path = await GetImageUrlUtils.getPath(ImageSource.gallery);
      uploadFile(File(path!));
    } finally {}
    update();
  }

  ///*拍照
  takePhoto(int index) async {
    try {
      String? path = await GetImageUrlUtils.getPath(ImageSource.camera);
      uploadFile(File(path!));
    } finally {}
    update();
  }

  uploadFile(File path) {
    MineApi.uploadFile(
        path,
        BaseCallback(onSuccess: (map) {
          UpdateImageBean updateImageBean = UpdateImageBean.fromJson(map);
          if (updateImageBean.data != null) {
            state.url = updateImageBean.data?.url;
            state.agreementList.add(SignAgreementBean(false, state.url ?? ''));
            update();
            LogUtils.d('图片路径  ${state.url}');
          }
        }, onError: (msg) {
          Future.delayed(const Duration(milliseconds: 500), () {
            ToastUtils.showToast(msg ?? "");
          });
        }));
  }

  void submit() {
    if (state.controller.text.isEmpty) {
      ToastUtils.showToast('请描述您的问题');
    } else if (state.phoneController.text.isEmpty) {
      ToastUtils.showToast('请输入您的联系方式');
    } else {
      // consult();
      String result='';
      if(state.agreementList.length==1){
        consult(result);
      }else{
        for (int i=0;i<state.agreementList.length;i++) {
          String? image=state.agreementList[i].getImage;
          if(image!.isNotEmpty){
            result += image + ',';
          }
        }
        if (result.endsWith(',')) {
          result = result.substring(0, result.length - 1);
        }
        consult(result);
        LogUtils.d('图片链接  $result');
      }
    }
  }

  consult(String filePath) {
    LoadingUtils.showLoading(Get.context!, isCanBack: false);
    MineApi.consult(
        state.question,
        state.phoneController.text,
        state.controller.text,
        filePath,
        BaseCallback(
            onSuccess: (map) {
              Get.back();
              FocusScope.of(Get.context!).requestFocus(FocusNode());
              ToastUtils.showToast('已提交');
              Get.back();
            },
            onError: (msg) {
              Future.delayed(const Duration(milliseconds: 500), () {
                Get.back();
                ToastUtils.showToast(msg ?? "");
              });
            }));
  }
}
